package Servlet;

import Service.StudentService.StudentService;
import Service.TeacherService.TeacherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
//http://localhost:8080/Online_Selection_Course_war_exploded/checkUser?userid=126&userName=admin
/*
负责教师学生 无密查询用户是否存在 数据接口
 */
@WebServlet(value = "/checkUser")
public class CheckUser extends HttpServlet {
    private StudentService studentService = new StudentService();
    private TeacherService teacherService = new TeacherService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String userId = request.getParameter("userId");
        String userName = request.getParameter("userName");

        PrintWriter out = response.getWriter();

        //尝试学生
        HttpSession session = request.getSession();
        if (studentService.selectStudentByIdAndName(userId,userName)){
            session.setAttribute("userId",userId);
            session.setAttribute("userName",userName);
            session.setAttribute("status","student");
            out.print(1);
        }else if(teacherService.selectTeacherByIdAndName(userId,userName)){
            session.setAttribute("userId",userId);
            session.setAttribute("userName",userName);
            session.setAttribute("status","teacher");
            out.print(1);
        }else{
            out.print(0);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
